<template>
  <Transition name="message">
    <div v-if="visible" :class="['message', `message-${type}`]">
      <span class="message-icon">{{ icon }}</span>
      <span class="message-text">{{ text }}</span>
    </div>
  </Transition>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'

const props = defineProps({
  text: {
    type: String,
    required: true
  },
  type: {
    type: String,
    default: 'info',
    validator: (value) => ['success', 'error', 'warning', 'info'].includes(value)
  },
  duration: {
    type: Number,
    default: 3000
  }
})

const visible = ref(false)

const icon = computed(() => {
  const icons = {
    success: '✓',
    error: '✕',
    warning: '⚠',
    info: 'ℹ'
  }
  return icons[props.type] || icons.info
})

onMounted(() => {
  visible.value = true

  if (props.duration > 0) {
    setTimeout(() => {
      visible.value = false
    }, props.duration)
  }
})
</script>

<style scoped>
.message {
  position: fixed;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  padding: 12px 20px;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  display: flex;
  align-items: center;
  gap: 8px;
  z-index: 10000;
  min-width: 300px;
  max-width: 500px;
}

.message-icon {
  font-size: 18px;
  font-weight: bold;
}

.message-text {
  font-size: 14px;
}

.message-success {
  background-color: #f0f9ff;
  border: 1px solid #91d5ff;
  color: #0958d9;
}

.message-error {
  background-color: #fff2f0;
  border: 1px solid #ffccc7;
  color: #cf1322;
}

.message-warning {
  background-color: #fffbe6;
  border: 1px solid #ffe58f;
  color: #d48806;
}

.message-info {
  background-color: #f5f5f5;
  border: 1px solid #d9d9d9;
  color: #262626;
}

.message-enter-active,
.message-leave-active {
  transition: all 0.3s ease;
}

.message-enter-from {
  opacity: 0;
  transform: translateX(-50%) translateY(-20px);
}

.message-leave-to {
  opacity: 0;
  transform: translateX(-50%) translateY(-20px);
}
</style>